/*
 *所有关于Aps_DistributedTask类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*Aps_DistributedTaskService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using DGZImp.Core.BaseProvider;
using DGZImp.Core.Extensions.AutofacManager;
using System.Linq;
using DGZImp.Core.Utilities;
using System.Linq.Expressions;
using DGZImp.Core.Extensions;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using DGZImp.ApsProject.IRepositories;
using DGZImp.ApsProject.Dtos;
using DGZImp.Core.EFDbContext;
using SqlSugar;
using DGZImp.Core.DbSqlSugar;
using DGZImp.BasProject.DomainModels;
using DGZImp.Core.Exceptions;
using DGZImp.ApsProject.DomainModels;
using DGZImp.Core.Enums;
using System.Drawing;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace DGZImp.ApsProject.Services
{
    public partial class Aps_DistributedTaskService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IAps_DistributedTaskRepository _repository;//访问数据库
        public ISqlSugarClient Db = AutofacContainerModule.GetService<ServiceDbContext>()?.SqlSugarClient;


        [ActivatorUtilitiesConstructor]
        public Aps_DistributedTaskService(
            IAps_DistributedTaskRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        public override PageGridData<Aps_DistributedTask> GetPageData(PageDataOptions options)
        {
            QueryRelativeExpression = (ISugarQueryable<Aps_DistributedTask> queryable) =>
            {
                queryable.Includes(x => x.Machine);

                return queryable;
            };
            GetPageDataOnExecuted = (PageGridData<Aps_DistributedTask> grid) =>
            {
                //可对查询的结果的数据操作
                grid.rows = grid.rows.OrderByDescending(a => a.CreateTime).ThenBy(a => a.SalesOrderId).ThenBy(a => a.Machine.Level).ThenByDescending(a => a.StartTime).ToList();

            };
            return base.GetPageData(options);
        }
        public async Task<PageGridData<Aps_DistributedTask>> getList(PageDataOptions options)
        {
            var size = options.Rows == 0 ? 99999 : options.Rows;
            RefAsync<int> total = 0;
            PageGridData<Aps_DistributedTask> pageGridData = new PageGridData<Aps_DistributedTask>();

            var query = Db.Set<Aps_DistributedTask>()
                  .LeftJoin<Bas_Machine>((a, b) => a.MachineId == b.MachineId)
                  .OrderByDescending(a => a.CreateTime)
                  .OrderBy(a => a.SalesOrderId)
                  .OrderBy(a => a.Machine.Level)
                  .OrderByDescending(a => a.StartTime);
            if (!string.IsNullOrEmpty(options.Wheres) && options.Wheres != "[]")
            {
                var filters = JsonConvert.DeserializeObject<List<SearchParameters>>(options.Wheres);
                foreach (var filter in filters)
                {
                    if (filter.Name == "SalesOrderId" && !string.IsNullOrEmpty(filter.Value))
                    {
                        query = query.Where(a => a.SalesOrderId == long.Parse(filter.Value));
                    }
                    else if (filter.Name == "MachineId" && !string.IsNullOrEmpty(filter.Value))
                    {
                        query = query.Where(a => a.MachineId == long.Parse(filter.Value));

                    }
                    else if (filter.Name == "MoCode" && !string.IsNullOrEmpty(filter.Value))
                    {
                        query = query.Where(a => a.MoCode == filter.Value);

                    }
                    else if (filter.Name == "GroupId" && !string.IsNullOrEmpty(filter.Value))
                    {
                        query = query.Where(a => a.GroupId == long.Parse(filter.Value));
                    }
                    else if (filter.Name == "ScheduleVersionId" && !string.IsNullOrEmpty(filter.Value))
                    {
                        query = query.Where(a => a.ScheduleVersionId == long.Parse(filter.Value));
                    }
                }
            }

            var list = await query.ToPageListAsync(options.Page, size, total);
            pageGridData.rows = list;
            pageGridData.total = total;
            return pageGridData;
        }

    }
}
